<?php

namespace Admin\Controller;
use Think\Controller;

class CaijiController extends BaseController{


	public function bj28(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		
		$caiji = M('caiji');
		$count = $caiji->where("game='bj28'")->count();
		$page = new \Think\Page($count,20);
		$show = $page->show();
		$list = $caiji->where("game='bj28'")->limit($page->firstRow.','.$page->listRows)->order("periodnumber desc")->select();

		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}

	public function jnd28(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		
		$caiji = M('caiji');
		$count = $caiji->where("game='jnd28'")->count();
		$page = new \Think\Page($count,20);
		$show = $page->show();
		$list = $caiji->where("game='jnd28'")->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();

		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}



	public function addbj28(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$data['periodnumber'] = I('periodnumber');
 				$data['awardnumbers'] = I('awardnumbers');
 				$data['awardtime'] = time();
 				$data['game'] = I('game');
 				$data['addtime'] = time();

 				//增加手动开奖结算
				$log = array(
					'username' => session('admin')['username'],
					'type' => 8,
					'addtime' => time(),
					'content' => "手动开奖，期号【{$data['periodnumber']}】,开奖号码【{$data['awardnumbers']}】"
				);
				M('admin_log')->add($log);

				//结算
				//开奖结果
				$map['awardnumbers'] = $data['awardnumbers'];
				$map['awardtime'] = $data['awardtime'];
				$map['time'] = strtotime($data['awardtime']);
				$map['periodnumber'] = $data['periodnumber'];
				$info = explode(',', $map['awardnumbers']);
			
				$map['number'] = serialize($info);
				
				$map['tema'] = $info[0]+$info[1]+$info[2] ;

				if($map['tema'] % 2 == 0){
					$map['tema_ds'] = '双';
				}
				else{
					$map['tema_ds'] = '单';
				}
			
				if($map['tema']>=14){
					$map['tema_dx'] = '大';
				}else{
					$map['tema_dx'] = '小';
				}
			
				if($info[0]>$info[1]){
					$map['zx'] = '庄';
				} else if($info[0] == $info[1]){
					$map['zx'] = '和';
				}else{
					$map['zx'] = '闲';
				}

				$map['q3'] = bj28_qzh(array($info[0],$info[1],$info[2]));

				$map['game'] = $data['game'];

				$current_number = $map;

				$number1 = explode(',', $current_number['awardnumbers']);
				$tema_number = $number1[0] + $number1[1] + $number1[2];
				if ($tema_number <= 13) {
					if ($tema_number%2 == 0) {
						$current_number['zuhe'] = '小双';
					} else {
						$current_number['zuhe'] = '小单';
					}
				} else {
					if ($tema_number%2 == 0) {
						$current_number['zuhe'] = '大双';
					} else {
						$current_number['zuhe'] = '大单';
					}
				}

				if ($tema_number >=0 && $tema_number <=5) {
					$current_number['jdx'] = '极小';
				} else if($tema_number >= 22 && $tema_number <=27) {
					$current_number['jdx'] = '极大';
				}  else {
					$current_number['jdx'] = '';
				}
				//当前局所有竞猜
				$today_time = strtotime(date('Y-m-d',time()));
				$game = $data['game'];
				if ($game == 'bj28') {
					$game_str = "game in ('bj28_1','bj28_2','bj28_3')";
				}else{
					$game_str = "game in ('jnd28_1','jnd28_2','jnd28_3')";
				}
				$list = M('order')->where("number = {$current_number['periodnumber']} && state = 1 && is_add = 0 && ".$game_str)->order("time ASC")->select();
				// var_dump("number = {$current_number['periodnumber']} && state = 1 && is_add = 0 && (".$game_str.")");
				// var_dump($list);exit;

				if($list){
					for($i=0;$i<count($list);$i++){
						$id = $list[$i]['id'];
						$userid = $list[$i]['userid'];
						if($list[$i]['t_id'] and C('fenxiao_set') == 2){
							M('user')->where("id = {$list[$i]['t_id']}")->setInc('yong', $list[$i]['del_points']*C('fenxiao')*0.01);
							M('user')->where("id = {$list[$i]['t_id']}")->setInc('t_add', $list[$i]['del_points']*C('fenxiao')*0.01);
							$fx_data = array(
								'uid' => $userid,
								't_uid' => $list[$i]['t_id'],
								'push_money' => $list[$i]['del_points'],
								'rate' => C('fenxiao')*0.01,
								'money' => $list[$i]['del_points']*C('fenxiao')*0.01,
								'time' => time()
							);
							M('push_money')->add($fx_data);
						}

						$set_add = M('order')->where("id={$id}")->setField(array('is_add'=>1));
						
						switch($list[$i]['type']){
							
							//大小单双  大100  小100  
							case 1:
								$start1 = substr($list[$i]['jincai'], 0,3);
								$starts1 = substr($list[$i]['jincai'],3);
								$num1 = 0;

								if ($start1 == '大' || $start1 == '小') {
									if ($start1 == $current_number['tema_dx']) {
										$num1 = 1;
									}
								} else {
									if ($start1 == $current_number['tema_ds']) {
										$num1 = 1;
									}
								}

								if($num1>0){
									if ($current_number['tema'] == '13' || $current_number['tema'] == '14') {
										$points1 = $num1*$starts1*C('bj28_dxds_md');
									} else {
										$points1 = $num1*$starts1*C('bj28_dxds');
									}
									
									$res1 = $this->add_points($id,$userid,$points1);
									if($res1){
										$this->send_msg('pointsadd',$points1,$userid);
									}
								}
								break;

							//组合  大单100  小100  
							case 2:
								$start2 = substr($list[$i]['jincai'], 0,6);
								$starts2 = substr($list[$i]['jincai'],6);
								$num2 = 0;

							
								if ($start2 == $current_number['zuhe']) {
									$num2 = 1;
								}

								if($num2>0){
									if ($current_number['tema'] == '13' || $current_number['tema'] == '14') {
										$points2 = $num2*$starts2*C('bj28_zuhe_md');
									} else {
										if ($start2 == '大单' || $start2 == '小双'){
											$points2 = $num2*$starts2*C('bj28_zuhe_1');
										} else {
											$points2 = $num2*$starts2*C('bj28_zuhe_2');
										}
									}
									
									$res2 = $this->add_points($id,$userid,$points2);
									if($res2){
										$this->send_msg('pointsadd',$points2,$userid);
									}
								}
								break;


							//极大小  极大100  
							case 3:
								$start3 = substr($list[$i]['jincai'], 0,6);
								$starts3 = substr($list[$i]['jincai'],6);
								$num3 = 0;
								if ($start3 == $current_number['jdx']) {
									$num3 = 1;
								}

								if($num3>0){
									$points3 = $num3*$starts3*C('bj28_jdx');
									$res3 = $this->add_points($id,$userid,$points3);
									if($res3){
										$this->send_msg('pointsadd',$points3,$userid);
									}
								}
								break;


							//庄闲和    庄100  和100
							case 4:
								$start4 = substr($list[$i]['jincai'], 0,3);
								$starts4 = substr($list[$i]['jincai'],3);


								$points4 = 0;

								
								if ($start4 == '庄' || $start4 == '闲') {
									if ($current_number['zx'] == '和') {
										$points4 = $starts4*1;
									} else if($start4 == $current_number['zx']){
										$points4 = $starts4*C('bj28_zx_1');
									}
								} else {
									if($start4 == $current_number['zx']){
										$points4 = $starts4*C('bj28_zx_2');
									}
								}
								if ($points4) {
									$res4 = $this->add_points($id,$userid,$points4);
									if($res4){
										$this->send_msg('pointsadd',$points4,$userid);
									}
								}
								
								
								break;


							//豹子对子顺子 100  白字100  
							case 5:
								$start5 = substr($list[$i]['jincai'], 0,6);
								$starts5 = substr($list[$i]['jincai'],6);
								$num5 = 0;
								if ($start5 == $current_number['q3']) {
									$num5 = 1;
								}

								if($num5>0){
									if ($start5 == '豹子') {
										$points5 = $num5*$starts5*C('bj28_bds_1');
									} else if($start5 == '顺子') {
										$points5 = $num5*$starts5*C('bj28_bds_2');
									} else if($start5 == '对子') {
										$points5 = $num5*$starts5*C('bj28_bds_3');
									} else if($start5 == '半顺') {
										$points5 = $num5*$starts5*C('bj28_bds_4');
									} else if($start5 == '杂六') {
										$points5 = $num5*$starts5*C('bj28_bds_5');
									}
									
									$res5 = $this->add_points($id,$userid,$points5);
									if($res5){
										$this->send_msg('pointsadd',$points5,$userid);
									}
								}
								break;


							//特码数字 3点100  
							case 6:
								$start6 = explode('点', $list[$i]['jincai']);

								$num6 = 0;
								if ($start6[0] == $current_number['tema']) {
									$num6 = 1;
								}

								if($num6>0){
									if ($start6[0] == '0' || $start6[0] == '27') {
										$points6 = $num6*$start6[1]*C('bj28_tema_0');
									} else if($start6[0] == '1' || $start6[0] == '26') {
										$points6 = $num6*$start6[1]*C('bj28_tema_1');
									} else if($start6[0] == '2' || $start6[0] == '25') {
										$points6 = $num6*$start6[1]*C('bj28_tema_2');
									} else if($start6[0] == '3' || $start6[0] == '24') {
										$points6 = $num6*$start6[1]*C('bj28_tema_3');
									} else if($start6[0] == '4' || $start6[0] == '23') {
										$points6 = $num6*$start6[1]*C('bj28_tema_4');
									} else if($start6[0] == '5' || $start6[0] == '22') {
										$points6 = $num6*$start6[1]*C('bj28_tema_5');
									} else if($start6[0] == '6' || $start6[0] == '21') {
										$points6 = $num6*$start6[1]*C('bj28_tema_6');
									} else if($start6[0] == '7' || $start6[0] == '20') {
										$points6 = $num6*$start6[1]*C('bj28_tema_7');
									} else if($start6[0] == '8' || $start6[0] == '19') {
										$points6 = $num6*$start6[1]*C('bj28_tema_8');
									} else if($start6[0] == '9' || $start6[0] == '18') {
										$points6 = $num6*$start6[1]*C('bj28_tema_9');
									} else if($start6[0] == '10' || $start6[0] == '17') {
										$points6 = $num6*$start6[1]*C('bj28_tema_10');
									} else if($start6[0] == '11' || $start6[0] == '16') {
										$points6 = $num6*$start6[1]*C('bj28_tema_11');
									} else if($start6[0] == '12' || $start6[0] == '15') {
										$points6 = $num6*$start6[1]*C('bj28_tema_12');
									} else if($start6[0] == '13' || $start6[0] == '14') {
										$points6 = $num6*$start6[1]*C('bj28_tema_13');
									}	

									$res6 = $this->add_points($id,$userid,$points6);
									if($res6){
										$this->send_msg('pointsadd',$points6,$userid);
									}
								}
								break;					
						}
					}
					$this->success('手动添加开奖成功，请查看竞猜记录该期的开奖结果!',U('Admin/Order/index'),1);
				}else{
					$this->error('当期未查找到下注记录');
				}
			}
			
		}else{
			$this->assign('game',I('game'));
			$this->display();
		}
	}


	public function add_points($order_id,$userid,$points){
		$res = M('user')->where("id = $userid")->setInc('points',$points);
		if($res){
			$res1 = M('order')->where("id = $order_id")->setField(array('is_add'=>'1','add_points'=>$points));
		}
		if($res && $res1){
			return 1;
		}
	}

	/*竞猜成功通知
	 * */
	public function send_msg($type,$points,$userid){
		
	} 

}

?>